package com.egao.common.module.validate.controller;

import com.egao.common.core.web.*;
import com.egao.common.module.validate.entity.ValidateRule;
import com.egao.common.module.validate.service.ValidateRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 验证规则控制器
 *
 * @author warmwind
 * @since 2024-11-21 14:04:53
 */
@Api(tags = "验证规则管理")
@RestController
@RequestMapping("/api/validate/rule")
public class ValidateRuleController extends BaseController {
    @Resource
    private ValidateRuleService ruleService;

    @PreAuthorize("hasAuthority('validate:rule:list')")
    @ApiOperation("分页查询验证规则")
    @GetMapping("/page")
    public PageResult<ValidateRule> page(HttpServletRequest request) {
        PageParam<ValidateRule> pageParam = new PageParam<>(request);
        pageParam.setDefaultOrder(null, new String[]{"create_time"});
        return new PageResult<>(ruleService.listPage(pageParam), pageParam.getTotal());
    }

    @PreAuthorize("hasAuthority('validate:rule:save')")
    @ApiOperation("添加验证规则")
    @PostMapping()
    public JsonResult save(@RequestBody ValidateRule rule) {
        return ruleService.insertValidateRule(rule);
    }

    @PreAuthorize("hasAuthority('validate:rule:update')")
    @ApiOperation("修改验证规则")
    @PutMapping()
    public JsonResult update(@RequestBody ValidateRule rule) {
        return ruleService.updateValidateRule(rule);
    }

    @PreAuthorize("hasAuthority('validate:rule:remove')")
    @ApiOperation("删除验证规则")
    @DeleteMapping("/{id}")
    public JsonResult remove(@PathVariable("id") Integer id) {
        return ruleService.deleteValidateRule(id);
    }

    @PreAuthorize("hasAuthority('validate:rule:list')")
    @ApiOperation("根据id查询验证规则")
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id") Integer id) {
        return ruleService.getValidateRuleRelById(id);
    }

}
